package mapEditor.view.mapPreview
{
	import flash.display.Sprite;
	import flash.display3D.Context3DRenderMode;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import render.Render;
	import render.display.RenderLayer;
	import render.events.RenderEvent;
	
	import stages.StageReflection;

	public class MapAreaPreviewView extends Sprite
	{
		/**
		 *渲染类 
		 */		
		private var _gpuRender:Render;
		/**
		 *渲染层 
		 */		
		private var _renderLayer:RenderLayer;
		/**
		 *渲染区域 
		 */		
		private var _renderRectangle:Rectangle;
		
		public function MapAreaPreviewView()
		{
			addEventListener(Event.ADDED_TO_STAGE, addedToStage);
		}
		
		private function addedToStage(event:Event):void 
		{
			_renderRectangle=new Rectangle(x, y, StageReflection.stageWidth-4, StageReflection.stageHeight-90-2);
			_gpuRender=new Render(stage, Context3DRenderMode.AUTO, 60, _renderRectangle);
			_gpuRender.addEventListener(RenderEvent.CONTEXT3D_CREATE, __onGPURenderCreate);
		}
		
		private function __onGPURenderCreate(evt:RenderEvent):void
		{
			//创建渲染类
			_renderLayer=new RenderLayer(_gpuRender.context3D);
			_gpuRender.addRenderLayer(_renderLayer);
			
			StageReflection.stage.addEventListener(Event.RESIZE, __onStageResize);
			addEventListener(Event.ENTER_FRAME, __onEnterFrame);
		}
		
		private function __onStageResize(evt:Event):void
		{
			_renderRectangle=new Rectangle(x, y, StageReflection.stageWidth-4, StageReflection.stageHeight-90-2);
			_gpuRender.renderRectangle=_renderRectangle;
			_gpuRender.resizeStage();
		}
		
		private function __onEnterFrame(evt:Event):void
		{
			_gpuRender.render();
		}
		
		public function dispose():void
		{
			removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
			StageReflection.stage.removeEventListener(Event.RESIZE, __onStageResize);
			removeEventListener(Event.ENTER_FRAME, __onEnterFrame);
			if(_gpuRender)
			{
				_gpuRender.removeEventListener(RenderEvent.CONTEXT3D_CREATE, __onGPURenderCreate);
				_gpuRender.dispose();
				_gpuRender=null;
			}
			_renderRectangle=null;
			if(parent)parent.removeChild(this);
		}
	}
}